/*
* Copyright (c) 2011 Connection Road.  All rights reserved.
* This program and the accompanying material are made available
* under the terms of the Eclipse Public Licnese v1.0 which is
* available at http://www.eclipse.org/legal/epl-v10.html
*/

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using CrSyncFilesForms.Properties;
using CrSyncFilesForms.Utils;
using CrSyncFilesUtils;

namespace CrSyncFilesForms.Forms
{
    public partial class CrSyncUiPanelAllFolderPairs : UserControl
    {

        public CrSyncUiPanelAllFolderPairs(List<FileSyncPairInfo> folderPairs)
        {
            InitializeComponent();
            _folderPairs = folderPairs;
        }

        public CrSyncUiPanelAllFolderPairs()
        {
            InitializeComponent();
        }

        public ListView DefaultView
        {
            get { return ListViewAllFolderPairs; }
            set { ListViewAllFolderPairs = value; }
        }

        public LinkLabel LinkScheduler
        {
            get { return LinkScheduleAllFolderPairs; }
            set { LinkScheduleAllFolderPairs = value; }
        }

        private List<FileSyncPairInfo> _folderPairs;


        public void CallBackLoad(object sender, EventArgs e)
        {
            InitializeAllFolderPairsListView();

            if ((ConfigurationManager.AppSettings["SchedulerEnabled"] != null) && (ConfigurationManager.AppSettings["SchedulerEnabled"].Equals("True")))
            {
                LinkScheduleAllFolderPairs.Visible = true;
                PictureBoxScheduler.Visible = true;
            }
            else
            {
                LinkScheduleAllFolderPairs.Visible = false;
                PictureBoxScheduler.Visible = false;
            }
        }

        public void LoadFolderPairs(List<FileSyncPairInfo> folderPairs)
        {
            _folderPairs = folderPairs;
            InitializeAllFolderPairsListView();
            
        }

        /// <summary>
        /// Populate the list view of available folder pairs
        /// </summary>
        public void InitializeAllFolderPairsListView()
        {
            ListViewAllFolderPairs.Clear();
            ListViewAllFolderPairs.HeaderStyle = ColumnHeaderStyle.Nonclickable;
            ListViewAllFolderPairs.View = View.Details;
            ListViewAllFolderPairs.Columns.Add("Active", 150);
            ListViewAllFolderPairs.Columns.Add("Name", 105);
            ListViewAllFolderPairs.Columns.Add("Left Folder", 150);
            ListViewAllFolderPairs.Columns.Add("Right Folder", 150);
            ListViewAllFolderPairs.CheckBoxes = true;



            if (_folderPairs != null)
            {
                foreach (var folderPair in _folderPairs)
                {
                    var viewItem = new ListViewItem { Checked = folderPair.ActiveForRunAll };
                    if (folderPair.Validate(ApplicationGlobals.PairManager) != CrSyncFileLocationResult.Valid)
                    {
                        viewItem.ForeColor = Color.Red;
                    }
                    viewItem.SubItems.Add(folderPair.FolderPairName);
                    viewItem.SubItems.Add(folderPair.LeftFolder.Path);
                    viewItem.SubItems.Add(folderPair.RightFolder.Path);
                    ListViewAllFolderPairs.Items.Add(viewItem);
                }
            }

        }

        public List<FileSyncPairInfo> GetAllActiveFolderPairs()
        {
            var checkedItems = ListViewAllFolderPairs.CheckedItems;

            return (from ListViewItem checkedItem in checkedItems
                    select checkedItem.SubItems[1]
                        into folderPair
                        where ApplicationGlobals.PairManager.HasFolderPairNamed(folderPair.Text)
                        select ApplicationGlobals.PairManager.CurrentFolderPair
                            into currentPair
                            where currentPair.Validate(ApplicationGlobals.PairManager) == CrSyncFileLocationResult.Valid
                            select currentPair).ToList();
        }
    }
}
